using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Xml;
using TMHuy.config;

namespace TMHuy
{
    class Util
    {
        public static string readFromFile(String filename)
        {
            try
            {
                StreamReader infile = new StreamReader(filename);
                String str = infile.ReadToEnd();
                infile.Close();
                return str;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static void writeStringToFile(String str, String filename)
        {
            try
            {
                StreamWriter outfile = new StreamWriter(filename);
                outfile.Write(str);
                outfile.Close();
            }
            catch (Exception ex)
            {
            }
        }

        public static int[] infDef = new int[] { 25, 35, 40, 66, 70, 80, 140, 380, 170, 440 };

        public static int getInfDef(String name)
        {
            if (name == "Rat" || name == "Rats") return 25;
            if (name == "Spider" || name == "Spiders") return 35;
            if (name == "Snake" || name == "Snakes") return 40;
            if (name == "Bat" || name == "Bats") return 66;
            if (name == "Wild Boar" || name == "Wild Boars") return 70;
            if (name == "Wolf" || name == "Wolves") return 80;
            if (name == "Bear" || name == "Bears") return 140;
            if (name == "Crocodile" || name == "Crocodiles") return 380;
            if (name == "Tiger" || name == "Tigers") return 170;
            if (name == "Elephant" || name == "Elephants") return 440;
            return 0;
        }

        public static int[] calDef = new int[] { 20, 40, 60, 50, 33, 70, 200, 240, 250, 520 };

        public static int getCalDef(String name)
        {
            if (name == "Rat" || name == "Rats") return 20;
            if (name == "Spider" || name == "Spiders") return 40;
            if (name == "Snake" || name == "Snakes") return 60;
            if (name == "Bat" || name == "Bats") return 50;
            if (name == "Wild Boar" || name == "Wild Boars") return 33;
            if (name == "Wolf" || name == "Wolves") return 70;
            if (name == "Bear" || name == "Bears") return 200;
            if (name == "Crocodile" || name == "Crocodiles") return 240;
            if (name == "Tiger" || name == "Tigers") return 250;
            if (name == "Elephant" || name == "Elephants") return 520;
            return 0;
        }

        public static void SerializeConfig(AllConfig obj, string filename)
        {
            System.Xml.XmlDocument doc = new XmlDocument();
            Type[] extraTypes = new Type[5] { typeof(Village), typeof(FarmList), typeof(TaskCheck), typeof(Route), typeof(Building) };
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(AllConfig), extraTypes);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            try
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                doc.Load(stream);
                writeStringToFile(doc.InnerXml, filename);
            }
            catch { throw; }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

        public static AllConfig DeSerializeConfig(string filename)
        {
            StreamReader streamReader = new StreamReader(filename);
            string serializedData = streamReader.ReadToEnd();
            streamReader.Close();
            AllConfig config = new AllConfig();
            Type[] extraTypes = new Type[5] { typeof(Village), typeof(FarmList), typeof(TaskCheck), typeof(Route), typeof(Building) };
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(AllConfig), extraTypes);
            XmlReader xReader = XmlReader.Create(new StringReader(serializedData));
            try
            {
                object obj = serializer.Deserialize(xReader);
                config = (AllConfig)obj;
            }
            catch
            {
                throw;
            }
            finally
            {
                xReader.Close();
            }
            return config;
        }

        public static void SerializeArrayList(ArrayList obj, string filename)
        {
            System.Xml.XmlDocument doc = new XmlDocument();
            Type[] extraTypes = new Type[2];
            extraTypes[0] = typeof(Village);
            extraTypes[1] = typeof(FarmList);
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList), extraTypes);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            try
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                doc.Load(stream);
                writeStringToFile(doc.InnerXml, filename);
            }
            catch { throw; }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

        public static ArrayList DeSerializeArrayList(string filename)
        {
            StreamReader streamReader = new StreamReader(filename);
            string serializedData = streamReader.ReadToEnd();
            streamReader.Close();
            ArrayList list = new ArrayList();
            Type[] extraTypes = new Type[2];
            extraTypes[0] = typeof(Village);
            extraTypes[1] = typeof(FarmList);
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList), extraTypes);
            XmlReader xReader = XmlReader.Create(new StringReader(serializedData));
            try
            {
                object obj = serializer.Deserialize(xReader);
                list = (ArrayList)obj;
            }
            catch
            {
                throw;
            }
            finally
            {
                xReader.Close();
            }
            return list;
        }

        public static ArrayList Shuffle(ArrayList source)
        {
            Random rnd = new Random();
            for (int inx = source.Count - 1; inx > 0; --inx)
            {
                int position = rnd.Next(inx);
                object temp = source[inx];
                source[inx] = source[position];
                source[position] = temp;
            }
            return source;
        }

        public static int getX(String xy)
        {
            string[] tmp = xy.Split(',');
            return Convert.ToInt32(tmp[0]);
        }

        public static int getY(String xy)
        {
            string[] tmp = xy.Split(',');
            return Convert.ToInt32(tmp[1]);
        }

        public static string taskTypeToString(int type)
        {
            switch (type)
            {
                default: return "Unknown";
            }
        }

        public static int timeToSeconds(string time)
        {
            try
            {
                string[] tmp = time.Split(':');
                int h = Convert.ToInt32(tmp[0]);
                int m = Convert.ToInt32(tmp[1]);
                int s = Convert.ToInt32(tmp[2]);
                return h * 3600 + m * 60 + s;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static string getRaidFileName(AllConfig config)
        {
            return config.account + "_" + config.server.Replace("http://", "").Replace(".", "_").Replace("/", "") + ".txt";
        }

        public static string getHashKey(int x, int y)
        {
            return x.ToString()+"|"+y.ToString();
        }
    }
}
